<?php

/**
 * Description of Group_model
 *
 * @author Administrator
 */
class Content_model extends CI_Model {

  //put your code here
  protected $table = '';
  protected $class = '';
  protected $base_lib = '';

  public function __construct() {
    parent::__construct();
    $this->class = get_class($this);
    $this->table = constant('DB_TABLE_' . strtoupper(substr($this->class, 0, strpos($this->class, '_model'))));
    $this->base_lib = new Base_lib($this->table);
  }

  public function create_obj($params) {
    $id = empty($params['id']) ? null : $params['id'];
    $obj = array('id' => $id);
    if ($id) {
      $obj = $this->db->get_where($this->table, array('id' => $id), 1, 0)->result_array();
      if (!empty($obj)) {
        $obj = $obj[0];
      }
    }
    foreach ($this->db->list_fields($this->table) as $field) {
      if ($field != 'id' && isset($params[$field])) {
        $obj[$field] = $params[$field];
      }
    }
    return $obj;
  }

  public function validate($obj) {
    $errors = array();
    if (empty($obj['key']))
      $errors['key'] = $this->lang->line('error.field.require', 'key');
    else if (strlen($obj['key']) < 3)
      $errors['key'] = $this->lang->line('error.field.min', array('key', 6));
    else if (!$this->base_lib->check_exist($obj['key'], 'key', $obj['id']))
      $errors['key'] = $this->lang->line('error.field.exist', array('Key', $obj['key']));
    return $errors;
  }

  protected function set_condition($where = array(), $or_where = array(), $where_in =array(), $or_where_in = array(), $where_not_in = array(), $or_where_not_in = array(), $like = array(), $or_like = array(), $not_like = array(), $or_not_like = array(), $group_by = array(), $is_distinct = false) {
    $flag = false;
    foreach (get_defined_vars() as $name => $value) {
      if (!empty($value) && is_array($value)) {
        foreach ($value as $field => $condition) {
          $this->db->$name($field, $condition);
          $flag = true;
        }
      }
    }
    if (!empty($is_distinct)) {
      $this->db->distinct();
      $flag = true;
    }
    return $flag;
  }
  
  public function to_string($object_or_id){
    if(!is_array($object_or_id)){
      $object_or_id = $this->get($object_or_id);
    }
    if ($object_or_id){
      return $object_or_id['description'];
    } else return '';
  }

  public function get($where = array(), $or_where = array(), $where_in =array(), 
          $or_where_in = array(), $where_not_in = array(), $or_where_not_in = array(), 
          $like = array(), $or_like = array(), $not_like = array(), $or_not_like = array() ) {
    if(isset($where)){
      if(!is_array($where)){
        $where = array('id' => $where);
      }
    } else $where = null;
    $flag = $this->set_condition($where, $or_where, $where_in, $or_where_in, $where_not_in, 
            $or_where_not_in, $like, $or_like, $not_like, $or_not_like);
    if(!$flag) return null;
    $object = $this->db->get($this->table, 1, 0)->result_array();
    if (!empty($object)) {
      return $object[0];
    } else {
      return null;
    }
  }
  
  public function get_content($key){
    $obj = $this->get(array('key' => $key));
    return $obj ? $obj['content'] : '';
  }

  public function get_all($where = array(), $or_where = array(), $where_in =array(), 
          $or_where_in = array(), $where_not_in = array(), $or_where_not_in = array(), 
          $like = array(), $or_like = array(), $not_like = array(), $or_not_like = array(), 
          $group_by = array(), $is_distinct = false, $order_by = array(), $max = 0, $offset = 0) {
    $this->set_condition($where, $or_where, $where_in, $or_where_in, $where_not_in, 
            $or_where_not_in, $like, $or_like, $not_like, $or_not_like, $group_by, $is_distinct);
    if(!empty($order_by)){
      if(is_array($order_by)){
        foreach($order_by as $sort => $order){
          $this->db->order_by($sort,$order);
        }
      } else {
        $this->db->order_by($order_by,'asc');
      }
    }
    if(empty($max)){
      return $this->db->get($this->table)->result_array();
    } else{
      return $this->db->get($this->table, $max, $offset)->result_array();
    }
  }

  public function count($where = array(), $or_where = array(), $where_in =array(), 
          $or_where_in = array(), $where_not_in = array(), $or_where_not_in = array(), 
          $like = array(), $or_like = array(), $not_like = array(), $or_not_like = array(), 
          $group_by = array(), $is_distinct = false) {
    $this->set_condition($where, $or_where, $where_in, $or_where_in, $where_not_in, 
            $or_where_not_in, $like, $or_like, $not_like, $or_not_like, $group_by, $is_distinct);
    return $this->db->count_all_results($this->table);
  }

  public function insert($params) {
    $object = $this->create_obj($params);
    $errors = $this->validate($object);
    if (empty($errors)) {
      $this->db->insert($this->table, $object);
      $id = $this->db->insert_id();
      if (!empty($id)) {
        return $id;
      } else
        return false;
    } else
      return $errors;
  }

  public function update($params, $where = array(), $or_where = array(), $where_in =array(), 
          $or_where_in = array(), $where_not_in = array(), $or_where_not_in = array(), 
          $like = array(), $or_like = array(), $not_like = array(), $or_not_like = array()) {
    if(!empty($where)){
      if(!is_array($where)){
        $where = array('id' => $where);
      }
    } else $where = null;
    $object = $this->create_obj($params);
    $errors = $this->validate($object);
    if (empty($errors)) {
      $this->set_condition($where, $or_where, $where_in, $or_where_in, $where_not_in, 
            $or_where_not_in, $like, $or_like, $not_like, $or_not_like);
      return $this->db->update($this->table, $object);
    } else return $errors;
  }
  
  public function save($params){
    if(!empty($params['id'])){
      return $this->update($params, array('id' => $params['id']));
    } else return $this->insert ($params);
  }
  
  public function delete($where = array(), $or_where = array(), $where_in =array(), 
          $or_where_in = array(), $where_not_in = array(), $or_where_not_in = array(), 
          $like = array(), $or_like = array(), $not_like = array(), $or_not_like = array()){
    if(!empty($where)){
      if(!is_array($where)){
        $where = array('id' => $where);
      }
    } else $where = null;
    $this->set_condition($where, $or_where, $where_in, $or_where_in, $where_not_in, 
            $or_where_not_in, $like, $or_like, $not_like, $or_not_like);
    $this->db->delete($this->table);
    return true;
  }
  
  public function truncate(){
    $this->db->truncate($this->table);
    return true;
  }
  
}